<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/19
 * Time: 15:45
 */

namespace app\company\controller;

use app\common\model\SuppliersCompany;
use app\common\model\SysConfig;
use app\common\model\SysNotice;
use app\common\model\SysNoticeLog;

class Common extends Admin
{
    public function basedata(){
        $data = array();
        json_return_code($data);
    }

    public function getUserInfo(){
        $con['id'] = input('opt_user_id');
        $model = new SuppliersCompany();
        $res =  $model->where($con)->find();
        $sysConfigModel = new SysConfig();
        $configInfo = $sysConfigModel->where(['name'=>'show_notice_day'])->find();
        $date1 = new \DateTime($res['due_time']);
        $date2 = new \DateTime(date('Y-m-d'));
        $interval = $date1->diff($date2);
        $res['due_state'] = $interval->days>$configInfo['val']['content']?0:($interval->days>0?1:2);
        if($res['due_state']){
            $logModel = new SysNoticeLog();
            $log_data = [
                'user_id'=>$res['id'],
                'user_type'=>$res['id'],
                'notice_id'=>0,
                'add_datetime'=>['>=',date('Y-m-d')]
            ];
            $res['user_state'] = $logModel->where($log_data)->count();
            if(!$res['user_state']){
                $log_data['add_datetime'] = date('Y-m-d H:i:s');
                $logModel->insert($log_data);
            }
        }
        json_return_code($res);
    }

    public function getUserNotice()
    {
        $user_id = input('opt_user_id');
        $suppliers_model = new SuppliersCompany();
        $create_time = $suppliers_model->where(['id'=>$user_id])->value('create_time');
        $where['add_datetime'] = ['>=',$create_time];
        $where['state'] = 1;
        $where['user_type'] = ['in',[0,2]];
        $model = new SysNotice();
        $info = $model->where($where)->order('id desc')->find();
        if($info){
            $param['user_id'] = $user_id;
            $param['user_type'] = 2;
            $param['notice_id'] = $info['id'];
            $logModel = new SysNoticeLog();
            $info['user_state'] = $logModel->where($param)->count();
            if(!$info['user_state']){
                $param['add_datetime'] = date('Y-m-d H:i:s');
                $logModel->insert($param);
            }
        }
        json_return_code($info);
    }

    public function getUserNoticeMore()
    {
        $user_id = input('opt_user_id');
        $suppliers_model = new SuppliersCompany();
        $create_time = $suppliers_model->where(['id'=>$user_id])->value('create_time');

        $param['user_id'] = $user_id;
        $param['user_type'] = 2;
        $logModel = new SysNoticeLog();
        $notice_ids = $logModel->where($param)->column('notice_id');

        $where['add_datetime'] = ['>=',$create_time];
        $where['state'] = 1;
        $where['user_type'] = ['in',[0,2]];
        $where['id'] = ['not in',$notice_ids];
        $model = new SysNotice();
        $info['count'] = $model->where($where)->count();
        $info['data_list'] = $model->where($where)->order('id desc')->select();
        if($info['count']){
            $save_data = [];
            $param['add_datetime'] = date('Y-m-d H:i:s');
            foreach ($info['data_list'] as $item){
                $param['notice_id'] = $item['id'];
                $save_data[] = $param;
            }
            $logModel->saveAll($save_data);
        }
        json_return_code($info);
    }
}